home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Paths Regions and Clipping / PathWarping / PathWarping.cs next >
Encoding:
Text File  |  2001-01-15  |  3.8 KB  |  128 lines

  1. //------------------------------------------
  2. // PathWarping.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8.  
  9. class PathWarping: Form
  10. {
  11.      MenuItem     miWarpMode;
  12.      GraphicsPath path;
  13.      PointF[]     aptfDest = new PointF[4];
  14.  
  15.      public static void Main()
  16.      {
  17.           Application.Run(new PathWarping());
  18.      }
  19.      public PathWarping()
  20.      {
  21.           Text = "Path Warping";
  22.  
  23.                // Create menu.
  24.  
  25.           Menu = new MainMenu();
  26.  
  27.           Menu.MenuItems.Add("&Warp Mode");
  28.           EventHandler ehClick = new EventHandler(MenuWarpModeOnClick);
  29.  
  30.           miWarpMode = new MenuItem("&" + (WarpMode)0, ehClick);
  31.           miWarpMode.RadioCheck = true;
  32.           miWarpMode.Checked = true;
  33.           Menu.MenuItems[0].MenuItems.Add(miWarpMode);
  34.  
  35.           MenuItem mi = new MenuItem("&" + (WarpMode)1, ehClick);
  36.           mi.RadioCheck = true;
  37.           Menu.MenuItems[0].MenuItems.Add(mi);
  38.  
  39.                // Create path.
  40.  
  41.           path = new GraphicsPath();
  42.  
  43.           for (int i = 0; i <= 8; i++)
  44.           {
  45.                path.StartFigure();
  46.                path.AddLine(0, 100 * i, 800, 100 * i);
  47.                path.StartFigure();
  48.                path.AddLine(100 * i, 0, 100 * i, 800);
  49.           }
  50.                // Initialize Point array.
  51.  
  52.           aptfDest[0] = new Point( 50,  50);
  53.           aptfDest[1] = new Point(200,  50);
  54.           aptfDest[2] = new Point( 50, 200);
  55.           aptfDest[3] = new Point(200, 200);
  56.      }
  57.      void MenuWarpModeOnClick(object obj, EventArgs ea)
  58.      {
  59.           miWarpMode.Checked = false;
  60.           miWarpMode = (MenuItem) obj;
  61.           miWarpMode.Checked = true;
  62.  
  63.           Invalidate();
  64.      }
  65.      protected override void OnMouseDown(MouseEventArgs mea)
  66.      {
  67.           Point pt;
  68.  
  69.           if (mea.Button == MouseButtons.Left)
  70.           {
  71.                if (ModifierKeys == Keys.None)
  72.                     pt = Point.Round(aptfDest[0]);
  73.                else if (ModifierKeys == Keys.Shift)
  74.                     pt = Point.Round(aptfDest[2]);
  75.                else
  76.                     return;
  77.           }
  78.           else if (mea.Button == MouseButtons.Right)
  79.           {
  80.                if (ModifierKeys == Keys.None)
  81.                     pt = Point.Round(aptfDest[1]);
  82.                else if (ModifierKeys == Keys.Shift)
  83.                     pt = Point.Round(aptfDest[3]);
  84.                else
  85.                     return;
  86.           }
  87.           else
  88.                return;
  89.  
  90.           Cursor.Position = PointToScreen(pt);
  91.      }
  92.      protected override void OnMouseMove(MouseEventArgs mea)
  93.      {
  94.           Point pt = new Point(mea.X, mea.Y);
  95.  
  96.           if (mea.Button == MouseButtons.Left)
  97.           {
  98.                if (ModifierKeys == Keys.None)
  99.                     aptfDest[0] = pt;
  100.                else if (ModifierKeys == Keys.Shift)
  101.                     aptfDest[2] = pt;
  102.                else
  103.                     return;
  104.           }
  105.           else if (mea.Button == MouseButtons.Right)
  106.           {
  107.                if (ModifierKeys == Keys.None)
  108.                     aptfDest[1] = pt;
  109.                else if (ModifierKeys == Keys.Shift)
  110.                     aptfDest[3] = pt;
  111.                else
  112.                     return;
  113.           }
  114.           else
  115.                return;
  116.  
  117.           Invalidate();
  118.      }
  119.      protected override void OnPaint(PaintEventArgs pea)
  120.      {
  121.           Graphics     grfx       = pea.Graphics;
  122.           GraphicsPath pathWarped = (GraphicsPath) path.Clone();
  123.           WarpMode     wm         = (WarpMode) miWarpMode.Index;
  124.  
  125.           pathWarped.Warp(aptfDest, path.GetBounds(), new Matrix(), wm);
  126.           grfx.DrawPath(new Pen(ForeColor), pathWarped);
  127.      }
  128. }